package com.hongxun.modules.api.controller;

import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.service.BMaterialQuantitiesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.api.entity.BMaterialEntity;
import com.hongxun.modules.api.service.BMaterialService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 配电材料信息表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-07-27 15:51:03
 */
@RestController
@RequestMapping("/api/bmaterial")
public class BMaterialController {
	@Autowired
	private BMaterialService bMaterialService;
	@Autowired
	private BMaterialQuantitiesService bMaterialQuantitiesService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
        query.put("filterSql",wheres+" ");
		if(query.get("mCode")!=null&&!query.get("mCode").toString().equals(""))
		{
			wheres+=" and  m_code like '%" + query.get("mCode").toString()+"%' ";
		}
		if(query.get("mName")!=null&&!query.get("mName").toString().equals(""))
		{
			wheres+=" and  m_name like '%" + query.get("mName").toString()+"%' ";
		}
		if(query.get("mLabel")!=null&&!query.get("mLabel").toString().equals(""))
		{
			wheres+=" and  m_label like '%" + query.get("mLabel").toString()+"%' ";
		}
		if(query.get("mIsmain")!=null&&!query.get("mIsmain").toString().equals(""))
		{
			wheres+=" and  m_ismain = '" + query.get("mIsmain").toString()+"' ";
		}
		if(query.get("mIshave")!=null&&!query.get("mIshave").toString().equals(""))
		{
			wheres+=" and ( m_ishave =0 or m_ishave  is null) ";
		}
		if(query.get("Btype")!=null&&!query.get("Btype").toString().equals(""))
		{
			wheres+=" and m_bigtype like '%" + query.get("Btype").toString()+"%' ";
		}

		if(query.get("mBigtype")!=null&&!query.get("mBigtype").toString().equals(""))
		{
			wheres+=" and m_bigtype like '%" + query.get("mBigtype").toString()+"%' ";
		}
		if(query.get("mMidtype")!=null&&!query.get("mMidtype").toString().equals(""))
		{
			wheres+=" and m_midtype like '%" + query.get("mMidtype").toString()+"%' ";
		}

		query.put("filterSql",wheres+" ");
		List<BMaterialEntity> bMaterialList = bMaterialService.queryList(query);
		int total = bMaterialService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bMaterialList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	@RequestMapping("/listwire")
    public R listwire(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		query.put("filterSql",wheres+" ");
		if(query.get("mCode")!=null&&!query.get("mCode").toString().equals(""))
		{
			wheres+=" and  m_code like '%" + query.get("mCode").toString()+"%' ";
		}
		if(query.get("mName")!=null&&!query.get("mName").toString().equals(""))
		{
			wheres+=" and  m_name like '%" + query.get("mName").toString()+"%' ";
		}
		wheres+=" and  m_bigtype = '导线' ";
	    query.put("filterSql",wheres+" ");
		List<BMaterialEntity> bMaterialList = bMaterialService.queryList(query);
		int total = bMaterialService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(bMaterialList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		BMaterialEntity bMaterial = bMaterialService.queryObject(guid);
		
		return R.ok().put("bMaterial", bMaterial);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("api:bmaterial:save")
	public R save(@RequestBody BMaterialEntity bMaterial){
		bMaterialService.save(bMaterial);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody BMaterialEntity bMaterial){
		bMaterialService.update(bMaterial);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		bMaterialQuantitiesService.deleteByMguid(guids);
		bMaterialService.deleteBatch(guids);
        return R.ok();
	}





	/**
	 * 	获取大类
	 */
	@RequestMapping("/getBigtype")
	public R getBigtype(){
		List<BMaterialEntity> bigtypeList = bMaterialService.queryForBigtype();
		return R.ok().put("bigtype",bigtypeList);
	}

	@RequestMapping("/getMidtype")
	public R getMidtype(@RequestParam Map<String, Object> params){
		String bigtype = params.get("mBigtype").toString();
		List<BMaterialEntity> midtypeList = bMaterialService.queryForMidtype(bigtype);
		return R.ok().put("midtype",midtypeList);
	}

}
